import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

import '../screens/home_screen.dart';
import '../src/rust/api/api.dart';
import 'entities.dart';

var repo = "";
var characters = <Character>[];
var charactersMap = <int, Character>{};
var items = <Item>[];
var itemsMap = <int, Item>{};
var weapons = <Weapon>[];
var weaponsMap = <int, Weapon>{};
var weaponAffixes = <WeaponAffix>[];
var weaponAffixesMap = <int, WeaponAffix>{};
var weaponStories = <WeaponStory>[];
var weaponStoriesMap = <int, WeaponStory>{};
var skillInfos = <SkillInfo>[];
var skillInfosMap = <String, SkillInfo>{};
var cardImages = <String>[];
var weaponAscensions = <WeaponAscension>[];
var weaponAscensionsMap = <int, WeaponAscension>{};
var banners = <BannerInfo>[];

String randomCardImage() {
  if (cardImages.isEmpty) {
    return "";
  }
  return cardImages[DateTime.now().microsecond % cardImages.length];
}

Future buildCacheAndGoHome(
  BuildContext context,
  Function(String str) fn,
) async {
  characters.clear();
  charactersMap.clear();
  items.clear();
  itemsMap.clear();
  weapons.clear();
  weaponsMap.clear();
  weaponAffixes.clear();
  weaponAffixesMap.clear();
  weaponStories.clear();
  weaponStoriesMap.clear();
  skillInfos.clear();
  skillInfosMap.clear();
  cardImages.clear();
  weaponAscensions.clear();
  weaponAscensionsMap.clear();
  repo = await repoDir();
  var charactersPath = '$repo/databack/characters';
  for (var line in await File(charactersPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var character = Character.fromJson(jsonDecode(line));
    if (character.icon.startsWith('..')) {
      continue;
    }
    characters.add(character);
    charactersMap[character.id] = character;
  }
  var itemsPath = '$repo/databack/items';
  for (var line in await File(itemsPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var item = Item.fromJson(jsonDecode(line));
    items.add(item);
    itemsMap[item.id] = item;
  }
  var weaponsPath = '$repo/databack/weapons';
  for (var line in await File(weaponsPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var weapon = Weapon.fromJson(jsonDecode(line));
    weapons.add(weapon);
    weaponsMap[weapon.id] = weapon;
  }
  var weaponAffixesPath = '$repo/databack/weapon_affixes';
  for (var line in await File(weaponAffixesPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var weaponAffix = WeaponAffix.fromJson(jsonDecode(line));
    weaponAffixes.add(weaponAffix);
    weaponAffixesMap[weaponAffix.id] = weaponAffix;
  }
  var weaponStoriesPath = '$repo/databack/weapon_stories';
  for (var line in await File(weaponStoriesPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var weaponStory = WeaponStory.fromJson(jsonDecode(line));
    weaponStories.add(weaponStory);
    weaponStoriesMap[weaponStory.id] = weaponStory;
  }
  var skillInfosPath = '$repo/databack/skill_infos';
  for (var line in await File(skillInfosPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var skillInfo = SkillInfo.fromJson(jsonDecode(line));
    skillInfos.add(skillInfo);
    skillInfosMap[skillInfo.id] = skillInfo;
  }
  var weaponAscensionsPath = '$repo/databack/weapon_ascensions';
  for (var line in await File(weaponAscensionsPath).readAsLines()) {
    if (line.isEmpty) {
      continue;
    }
    var weaponAscension = WeaponAscension.fromJson(jsonDecode(line));
    weaponAscensions.add(weaponAscension);
    weaponAscensionsMap[weaponAscension.id] = weaponAscension;
  }
  characters.sort((a, b) {
    if (a.id > 500 != b.id > 500) {
      return a.id > 500 ? 1 : -1;
    }
    return -a.id.compareTo(b.id);
  });
  items.sort((a, b) => -a.id.compareTo(b.id));
  weapons.sort((a, b) {
    if (a.rank != b.rank) {
      return -a.rank.compareTo(b.rank);
    }
    return -a.equipaffixid.compareTo(b.equipaffixid);
  });
  weaponAffixes.sort((a, b) => -a.id.compareTo(b.id));
  weaponStories.sort((a, b) => -a.id.compareTo(b.id));
  cardImages.clear();
  await for (var pic in Directory('$repo/pics/ItemPic').list()) {
    if (pic is File && pic.path.contains('/UI_NameCardPic')) {
      cardImages.add(pic.path);
    }
  }
  var bannersPath = '$repo/databack/banners';
  banners = List.from(jsonDecode(await File(bannersPath).readAsString())).map((e) => BannerInfo.fromJson(e)).toList();
  fn("OK");
  Navigator.pushReplacement(
    context,
    MaterialPageRoute(builder: (context) => const HomeScreen()),
  );
}
